/**
 * 
 */
package ar.edu.utn.frgp.banco.utils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

/**
 * @author PUSDERKIS
 *
 */
public class RequestConverterUtils {
	
	
	public static boolean canConvertBigDecimal(HttpServletRequest req, String atrib){
		Double numero;
		try{
			numero=Double.valueOf(req.getParameter(atrib));
			new BigDecimal(numero);
		} catch (Exception e) {
			return false;
		}		
		return true;
	}
	public static BigDecimal convertBigDecimal(HttpServletRequest req, String atrib){
		Double numero;
		try{
			numero=Double.valueOf(req.getParameter(atrib));
			return new BigDecimal(numero);
		} catch (Exception e) {
			return null;
		}		
	}
	
	// by Jonni
	public static boolean canConvertInteger(HttpServletRequest req, String atrib){
		try{
			Integer.valueOf(req.getParameter(atrib));
		} catch (Exception e) {
			return false;
		}		
		return true;
	}
	
	public static Integer convertInteger(HttpServletRequest req, String atrib){
		try {
			return Integer.valueOf(req.getParameter(atrib));
		} catch (Exception e) {
			return null;
		} 
	}
	
	public static boolean canConvertLong(HttpServletRequest req, String atrib){
		try{
			Long.valueOf(req.getParameter(atrib));
		} catch (Exception e) {
			return false;
		}		
		return true;
	}
	
	public static Long convertLong(HttpServletRequest req, String atrib){
		try {
			return Long.valueOf(req.getParameter(atrib));
		} catch (Exception e) {
			return null;
		} 
	}
	
	public static boolean canConvertDate(HttpServletRequest req, String param) {
		if (req.getParameter(param) == null) return false;
	  try {
	      SimpleDateFormat formatoFecha = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
	      formatoFecha.setLenient(false);
	      formatoFecha.parse(req.getParameter(param));
	  } catch (ParseException e) {
	      return false;
	  }
	  return true;
	}
	
	public static java.util.Date convertDate(HttpServletRequest req, String param) {
		if (req.getParameter(param) == null) return null;
	  try {
	      SimpleDateFormat formatoFecha = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
	      formatoFecha.setLenient(false);
	      return formatoFecha.parse(req.getParameter(param));
	      
	  } catch (ParseException e) {
	      
	  }
	  return null;
	}

}
